#!/usr/bin/env bash
set -eu -o pipefail

UAA_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"

db_variant() {
  local db_type="${1}"

  if [[ "${db_type}" == "percona" ]]; then
    echo "mysql"
  else
    echo "${db_type}"
  fi
}

gradle() {
  local gradle_command="./gradlew ${*}"
  cat <<COMMAND
Running:
  ${gradle_command}
COMMAND

  command pushd "${UAA_DIR}" >/dev/null # silence output
  ${gradle_command}
  command popd >/dev/null # silence output
}

db="$(db_variant "${DB-hsqldb}")"
TESTENV="${TESTENV-${db},default}"

if [[ -x /bin/start-slapd ]]; then /bin/start-slapd; fi
if [[ -x "/bin/start-${db}" ]]; then "/bin/start-${db}"; fi

gradle --stacktrace \
  "-Dspring.profiles.active=${TESTENV}" \
  "${@}"
